package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.po.Clazz;
import com.itheima.domain.po.Emp;
import com.itheima.domain.po.Student;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.ClazzOption;
import com.itheima.pojo.JobOption;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Primary
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 统计员工性别信息
     * 1: 男性, 2: 女性
     */
    @Override
    public List<Map> empGenderData() {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("gender", "count(*) as value");
        queryWrapper.groupBy("gender");
        List<Map<String, Object>> maps = empMapper.selectMaps(queryWrapper);

        // 数字转换成性别
        return maps.stream().map(map -> {
            Map<String, Object> resultMap = new HashMap<>();
            Integer gender = (Integer) map.get("gender");
            resultMap.put("name", gender == 1 ? "男性" : "女性");
            resultMap.put("value", map.get("value"));
            return resultMap;
        }).collect(Collectors.toList());
    }

    /**
     * 统计员工职位信息
     * 1: 班主任, 2: 讲师, 3: 学工主管, 4: 教研主管, 5: 咨询师
     */
    @Override
    public JobOption empJobData() {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("job", "count(*) as value");
        queryWrapper.groupBy("job");
        queryWrapper.isNotNull("job");
        List<Map<String, Object>> maps = empMapper.selectMaps(queryWrapper);

        // 数字转换成职位
        List<Object> name = maps.stream().map(map -> {
            Integer job = (Integer) map.get("job");
            switch (job) {
                case 1:
                    return "班主任";
                case 2:
                    return "讲师";
                case 3:
                    return "学工主管";
                case 4:
                    return "教研主管";
                case 5:
                    return "咨询师";
                default:
                    return "其他";
            }
        }).collect(Collectors.toList());

        List<Object> value = maps.stream().map(map -> map.get("value")).collect(Collectors.toList());

        return new JobOption(name, value);
    }

    /**
     * 学员学历统计
     * 1: 初中, 2: 高中, 3: 大专, 4: 本科, 5: 硕士, 6: 博士
     */
    @Override
    public List<Map> studentDegreeData() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("degree", "count(*) as value");
        queryWrapper.groupBy("degree");
        List<Map<String, Object>> maps = studentMapper.selectMaps(queryWrapper);

        // 数字转换成学历
        return maps.stream().map(map -> {
            Map<String, Object> resultMap = new HashMap<>();
            Integer degree = (Integer) map.get("degree");
            switch (degree) {
                case 1:
                    resultMap.put("name", "初中");
                    break;
                case 2:
                    resultMap.put("name", "高中");
                    break;
                case 3:
                    resultMap.put("name", "大专");
                    break;
                case 4:
                    resultMap.put("name", "本科");
                    break;
                case 5:
                    resultMap.put("name", "硕士");
                    break;
                case 6:
                    resultMap.put("name", "博士");
                    break;
                default:
                    resultMap.put("name", "其他");
                    break;
            }
            resultMap.put("value", map.get("value"));
            return resultMap;
        }).collect(Collectors.toList());
    }

    /**
     * 班级人数统计
     */
    @Override
    public ClazzOption clazzCountData() {
        // 先查询所有班级
        List<Clazz> clazzes = clazzMapper.selectList(null);

        // nameList 存储班级名称  valueList存储对应班级的学生人数
        List<Object> nameList = new ArrayList<>();
        List<Object> valueList = new ArrayList<>();

        // 对每个班级统计学生人数
        for (Clazz clazz : clazzes) {
            nameList.add(clazz.getName());

            // 统计该班级的学生人数
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("clazz_id", clazz.getId());
            Long count = studentMapper.selectCount(studentQueryWrapper);
            valueList.add(count);
        }

        return new ClazzOption(nameList, valueList);
    }
}